
package com.eyedog.video;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.eyedog.library.LibraryController;
import com.eyedog.video.entity.ApiEntity;
import com.eyedog.video.http.HttpClient;
import com.eyedog.video.http.ICallback;
import com.fm.openinstall.OpenInstall;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class VApplication extends Application {

    private final String TAG = "VApplication";

    private static Context mContext;

    public static String api_url;

    public LibraryController libraryController;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        OpenInstall.init(this);
        closeAndroidPDialog();
        loadApiUrl();
        libraryController = new LibraryController();
        libraryController.onCreate(mContext);
    }

    public static Context getAppContext() {
        return mContext;
    }

    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadApiUrl() {
        HttpClient.getInstance().requestApi(new ICallback<ApiEntity>() {
            @Override
            public void onLoadSuccess(ApiEntity apiEntity) {
                if (apiEntity != null) {
                    api_url = apiEntity.url;
                    Log.i(TAG, "load apiUrl Success:" + api_url);
                }
            }

            @Override
            public void onLoadFailed(String err) {
            }
        });
    }

}
